package educar.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import educar.controllers.CorreccionController;
import educar.controllers.DatosController;
import educar.controllers.IController;
import educar.controllers.LoginController;
import educar.controllers.NotaController;
import educar.controllers.SolucionController;
import educar.db.JPA;
import educar.models.Session;

public class SolucionView extends JPanel implements IView {
	private JFrame jfWindow;
	JLabel label1;
	JLabel label0;
	JTextField textField1;
	 JLabel label2;
 JTextField textField2;
	 JButton button1;
	  javax.swing.JComboBox jComboBox1;
	  javax.swing.JComboBox jComboBox2;
	  static String user;
	  String dniAlum;
	  String codMat;
	  String matInscrip [];
	  String actMat[];
	  
	public SolucionView() {
		jfWindow = new JFrame();
		Container container = jfWindow.getContentPane();

		setupMainContainer(container);

		jfWindow.pack();
		jfWindow.setVisible(true);
		jfWindow.setResizable(false);
		jfWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	private void setupMainContainer(Container container) {
		container.setLayout((LayoutManager) new BoxLayout(jfWindow
				.getContentPane(), BoxLayout.Y_AXIS));

		((JComponent) container).setBorder(new EmptyBorder(0, 50,50, 50));

		JLabel lblWelcome = new JLabel(" Solucion a actividad: ");
		lblWelcome.setAlignmentX(Component.CENTER_ALIGNMENT);
		lblWelcome.setFont(new Font("Serif", Font.BOLD, 16));
		lblWelcome.setBorder(new EmptyBorder(0, 10, 10, 10));
		container.add(lblWelcome);

		JPA jpa = new JPA();
		user = (Session.getCurrentUser().getUsername());
		JLabel label0 = new JLabel ("Materia:");
		try {
		dniAlum =jpa.getConsulta("users", "dni", "username='"+user+"'")[0];
		codMat = jpa.getConsulta("inscripcion_materia", "codigo", "dni='"+dniAlum+"'")[0];
		matInscrip = jpa.getConsulta("materia", "nombre", "codigo='"+codMat+"'");
		actMat = jpa.getConsulta("actividad", "nombre", "codigo='"+codMat+"'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jComboBox1 = new javax.swing.JComboBox();
		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(matInscrip));
	       jComboBox1.setName("jComboBox1"); 
	       JPanel usernamePane2 = new JPanel();
			usernamePane2.setLayout(new BoxLayout(usernamePane2, BoxLayout.LINE_AXIS));
			usernamePane2.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));					
			usernamePane2.add(label0);	
			usernamePane2.add(jComboBox1);					
			container.add(usernamePane2);
		//-----------------------------------------------------------	
			JLabel label1 = new JLabel ("Actividad:"); 
			jComboBox2 = new javax.swing.JComboBox();
			jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(actMat));
		       jComboBox2.setName("jComboBox2"); 
		       JPanel usernamePane4 = new JPanel();
				usernamePane4.setLayout(new BoxLayout(usernamePane4, BoxLayout.LINE_AXIS));
				usernamePane4.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));					
				usernamePane4.add(label1);	
				usernamePane4.add(jComboBox2);					
				container.add(usernamePane4);
			

		//------------------------------------------------------
	   JLabel label2 = new JLabel ("Fecha:"); 
		textField2 = new JTextField(20);
		JPanel usernamePane3 = new JPanel();
		usernamePane3.setLayout(new BoxLayout(usernamePane3, BoxLayout.LINE_AXIS));
		usernamePane3.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		//usernamePane2.add(Box.createHorizontalGlue());
		usernamePane3.add(label2);	
		usernamePane3.add(Box.createRigidArea(new Dimension(15, 0)));
		usernamePane3.add(textField2);	
		container.add(usernamePane3);
		
		//---- button1 ----
				
		JButton bsolucion = new JButton("Enviar");
	bsolucion.setAlignmentX(Component.RIGHT_ALIGNMENT);
		container.add(bsolucion);
		
		LoginListener buttonListener = new LoginListener();
	IController loginController = new SolucionController();
	loginController.setView(this);
	buttonListener.associate(bsolucion, loginController);

	}
	
	public String getFecha() {
        return textField2.getText();
}
	 
	 
	 public void present(String model) {
			JOptionPane.showMessageDialog(null, model);
		}
	 
	 public String getMateria(){
			
         return (String) jComboBox1.getSelectedItem();
     }

	 public String getActividad(){
			
         return (String) jComboBox2.getSelectedItem();
     }
	
	public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (Exception ex) {
					// ignore
				}
               javax.swing.JFrame frame = new javax.swing.JFrame("Datos");
                frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                frame.getContentPane().add(new SolucionView());
                frame.pack();
                frame.setVisible(false);
                            
            }
        });
    }
}